package com.cnsoft.portal.saas;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.cache.session.SessionHelper;
import org.cnsoft.framework.json.JSONObject;
import org.cnsoft.framework.support.MyPageControllerSupport;
import org.cnsoft.framework.utils.DateHelper;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cnsoft.persistent.company2.C211020SupplierService.C211020SupplierServiceDBO;
import com.cnsoft.persistent.company2.C212010CompanyApplyService.C212010CompanyApplyServiceDBO;
import com.cnsoft.persistent.company2.C212010CompanyApplyService.C212010CompanyApplyServiceDao;
import com.cnsoft.portal.saas.app.SaasAPPInfoCacheService;

/**
 * 工作台首页
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * 
 */
@Controller
// @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping(value = "/pc/1.0/saas")
public class PageSaasCenterController extends MyPageControllerSupport {
	@Resource
	private C212010CompanyApplyServiceDao daoC212010CompanyApplyService;

// 应用管理
	@RequestMapping(value = "/center-command.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView doAdminCenterCommandPOST() throws Exception {
		ModelAndView modelAndView = getModelAndView("/saas/saas-center");

		// 获得应用开通权限
		C212010CompanyApplyServiceDBO companyApplyServiceParam = new C212010CompanyApplyServiceDBO();
		companyApplyServiceParam.setCompanyId(SessionHelper.currentCompanyId());
		List<C212010CompanyApplyServiceDBO> datas = daoC212010CompanyApplyService.doSelectData(companyApplyServiceParam);
		HashMap<String, Integer> appRoolDatas = new HashMap<String, Integer>();
		if (EmptyHelper.isEmpty(datas)) {
			//不处理
		} else {
			//封装后返回
			for (int i = 0; i < datas.size(); i++) {
				companyApplyServiceParam = datas.get(i);
				appRoolDatas.put("serviceId" + companyApplyServiceParam.getServiceId(), 1);
			}
		}
		modelAndView.addObject("rools", JSONObject.toJsonString(appRoolDatas));

		UserBean currentUser = SessionHelper.currentUser();

		modelAndView.addObject("DDD", DateHelper.currentTimeMillis2());
		modelAndView.addObject("token", SessionHelper.currentToken());
		modelAndView.addObject("loginer", currentUser);

		//系统应用定义1通用2扩展3收费
		Map<String, List<C211020SupplierServiceDBO>> serviceAll = mySaasAPPInfoCacheService.loadSupplierServiceAll();
		modelAndView.addObject("communalService", serviceAll.get(ONE));
		modelAndView.addObject("expandService", serviceAll.get(TWO));
		modelAndView.addObject("chargeService", serviceAll.get(THREE));
		
		return modelAndView; // 返回result对象给前端
	}
	
	@Resource
	private SaasAPPInfoCacheService mySaasAPPInfoCacheService;

// 成员管理
	@RequestMapping(value = "/center-member.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView doAdminCenterMemberPOST() throws Exception {
		ModelAndView modelAndView = getModelAndView("/saas/saas-member");

		UserBean currentUser = SessionHelper.currentUser();

		modelAndView.addObject("DDD", DateHelper.currentTimeMillis2());
		modelAndView.addObject("token", SessionHelper.currentToken());
		modelAndView.addObject("loginer", currentUser);

		return modelAndView; // 返回result对象给前端
	}

// 钉钉管理
	@RequestMapping(value = "/center-order.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView doAdminCenterOrderPOST() throws Exception {
		ModelAndView modelAndView = getModelAndView("/saas/saas-order");

		UserBean currentUser = SessionHelper.currentUser();

		modelAndView.addObject("DDD", DateHelper.currentTimeMillis2());
		modelAndView.addObject("token", SessionHelper.currentToken());
		modelAndView.addObject("loginer", currentUser);

		return modelAndView; // 返回result对象给前端
	}

/////////////////////////////////////////////////////////////////////////////////////////////////
// 管理后台首页
	@RequestMapping(value = "/open-app.do", method = RequestMethod.POST)
	public ModelAndView doenunPOST(String popParam) throws Exception {
		logger.debug("C212010CompanyApplyServiceDBO.param=====>>>>" + popParam);
		C212010CompanyApplyServiceDBO param = JSONObject.parseObject(popParam, C212010CompanyApplyServiceDBO.class);
		ModelAndView modelAndView = getModelAndView("/saas/open-app");
// 基础信息
		modelAndView.addObject("service", param);

		return modelAndView; // 返回result对象给前端
	}

// 数据保存
	@RequestMapping(value = "/app/open", method = RequestMethod.POST)
	public ModelAndView doSave(C212010CompanyApplyServiceDBO param, RESTResultBean<String> message) throws Exception {
// 输出参数日志
		logger.debug("param=====>>>>" + param.toJsonString());
		param.setSupplierCompanyId(SYSTEM);
		param.setCompanyId(SessionHelper.currentCompanyId());
		daoC212010CompanyApplyService.doInsertSelective(param);
		message.setResult(0, "2:应用开通成功");

// 设定展示界面
		ModelAndView modelAndView = getModelAndView("/common/pageform-saas");
// 页面提示信息设定
		modelAndView.addObject("message", message);
// 目标展示页面
		modelAndView.addObject("target", "/pc/1.0/saas/center-command.do");
		return modelAndView;
	}

}
